package com.jk11a.shangdaast.notice;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.jk11a.shangdaast.R;
import com.jk11a.shangdaast.dao.LibraryDao;
import com.jk11a.shangdaast.dao.NoticeDao;
import com.jk11a.shangdaast.library.Book;
import com.jk11a.shangdaast.library.lib_book;

public class Homework extends Activity {

	private Thread homeworkThread;
	 private static final int MSG_SUCCESS = 0; //获取成功
	 private static final int MSG_FAILURE = 1; //获取失败
		private SharedPreferences sp=null;
		private String FILE = "saveUserNamePwd";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notice_homework);
		sp = this.getSharedPreferences(FILE, Context.MODE_PRIVATE);
		if(homeworkThread==null){
			homeworkThread=new Thread(runnable);
			homeworkThread.start();
		}
		else {
			Toast.makeText(getApplication(), "popbook thread 已启动",  Toast.LENGTH_LONG).show();
		}

	}
	Runnable runnable=new Runnable() {
		// 重写run()方法，此方法在新的线程中运行
		@Override
		public void run() {
			// TODO Auto-generated method stub
			NoticeDao noticeDao=new NoticeDao();
			List<NoticeBean> notices=noticeDao.gethomework(sp.getInt("id", 0));
			// 获取 成功，向UI线程发送MSG_SUCCESS标识和books对象
			if(notices.size()!=0){
			mHandler.obtainMessage(MSG_SUCCESS,notices).sendToTarget();
			}
			else{
				// 获取 失败，向UI线程发送MSG_FAILURE标识
				 mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
				 return;
			}
		}
	};
	private Handler mHandler=new Handler(){
		// 重写handleMessage()方法，此方法在UI线程运行
		@Override
		public void handleMessage(Message msg){
			switch (msg.what) {
			case MSG_SUCCESS:
				List<NoticeBean> notices=(List<NoticeBean>)msg.obj;
				popnoticelist	(notices);  
				break;

			case MSG_FAILURE:
				Toast.makeText(getApplication(), "获取作业失败",  Toast.LENGTH_LONG).show();
				break;
			}
		}
	};
	private void popnoticelist(List<NoticeBean> notices){
		int j = 1;
		int i = 0;
        for (i = 0; i < notices.size(); i++) {
		RelativeLayout rLayout = new RelativeLayout(this);
		if(i==0){
			rLayout.setBackgroundResource(R.drawable.preference_first_item);
			rLayout.setTop(14);
		}else{
			rLayout.setBackgroundResource(R.drawable.preference_item);
		}
		rLayout.setClickable(true);
		setonclick(this, rLayout, notices, i);   //设置onclick 事件
		rLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
		
		rLayout.setGravity(Gravity.CENTER);
		TextView tv = new TextView(this);
	
		tv.setText(notices.get(i).getTitle()+"			"+notices.get(i).getPublishtime());
		tv.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv.setTextColor(Color.rgb(0, 0, 0));
		rLayout.addView(tv);
		((LinearLayout) this.findViewById(R.id.homeworklist)).addView(rLayout);
		
	}

}
	public void setonclick(Homework homework,RelativeLayout rlayout,final List<NoticeBean> notices,final int i){
		rlayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent();
				intent.putExtra("title", notices.get(i).getTitle());
				intent.putExtra("content", notices.get(i).getMessage());
				intent.putExtra("writer", notices.get(i).getWriterid()+"");
				intent.setClass(Homework.this, Showonehomework.class);
				startActivity(intent);
			}
		});
	}
}
